/****************************************************************************
 * 2025.5 KS
 ****************************************************************************/

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using System.Linq;
using UnityEngine.U2D;

namespace ProjectSurvivor
{
	public partial class TreasureChestPanel : UIElement, IController//获取目前升级项
	{
		ResLoader mResLoader = ResLoader.Allocate();
		private void Awake()
		{
			BtnSure.onClick.AddListener(() =>
			{
				Time.timeScale = 1.0f;
				this.Hide();
			});

		}
		void OnEnable()
		{
			//1.判断是否有匹配的 没合成的
			//2.判断是否有没完级的
			//升级系统
			var expUpgradeSystem = this.GetSystem<ExpUpgradeSystem>();

			var matchedPairedItems = expUpgradeSystem.Items.Where(item =>
			{
				if (item.CurrentLevel.Value >= 7)
				// if (item.CurrentLevel.Value >= 1&&item.PairedName.IsNotNullAndEmpty())
				{
					var containsInPair = expUpgradeSystem.Pairs.ContainsKey(item.Key);
					var pairedItemKey = expUpgradeSystem.Pairs[item.Key];
					var pairedItemStartUpgrade = expUpgradeSystem.Dictionary[pairedItemKey].CurrentLevel.Value > 0;
					var pairedUnlocked = expUpgradeSystem.PairedProperties[item.Key].Value;
					return containsInPair && pairedItemStartUpgrade && !pairedUnlocked;
				}
				return false;
			});
			if (matchedPairedItems.Any())
			{
				var item = matchedPairedItems.ToList().GetRandomItem();
				Content.text = "<b>" + item.PairedName + "</b>\n"+item.PairedDescription;
				while (!item.UpgradeFinish)
				{
					item.Upgrade();
				}
				Icon.sprite = mResLoader.LoadSync<SpriteAtlas>("icon").GetSprite(item.PairedIconName);
				Icon.Show();
				expUpgradeSystem.PairedProperties[item.Key].Value = true;
			}
			else
			{
				//                                                 宝箱给一次免费升级 针对已经解锁的技能   没有升级完成
				var upgradeItems = expUpgradeSystem.Items.Where(item => item.CurrentLevel.Value > 0 && !item.UpgradeFinish).ToList();
				//任何一个
				if (upgradeItems.Any())
				{
					var item = upgradeItems.GetRandomItem();
					Content.text = item.Description;
					Icon.sprite = mResLoader.LoadSync<SpriteAtlas>("icon").GetSprite(item.IconName);
					Icon.Show();
					item.Upgrade();

				}
				else
				{
					//没有可以升级的，随机奖励
					if (Global.HP.Value < Global.MaxHp.Value)
					{
						if (Random.Range(0, 1.0f) < 0.2f)
						{
							Content.text = "恢复1血量";
							AudioKit.PlaySound("Hp");
							Global.HP.Value++;
							Icon.Hide();
							return;
						}

						Content.text = "增加50金币";
						Global.Coin.Value += 50;
						Icon.Hide();
					}
				}
			}
		}

		protected override void OnBeforeDestroy()
		{
			mResLoader.Recycle2Cache();
			mResLoader = null;
		}

		public IArchitecture GetArchitecture()
		{
			return Global.Interface;
		}
	}
}